package com.zhongjian.technologycal.mall.activity.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.main.bean.BreifBean;
import com.zhongjian.technologycal.mall.activity.main.bean.CourseHomeBean;
import com.zhongjian.technologycal.mall.activity.web.WebViewMarkActivity;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.LogUtils;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/11/14.
 * 课程详情
 */

public class CourseDetailsNoPayActivity extends BaseActivity {
    public static final String CATEGORY_NAME = "com.nanchen.aiyagirl.module.category.CategoryFragment.CATEGORY_NAME";
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.nopay_course_details_iv_icon)
    ImageView nopay_course_details_iv_icon;
    @BindView(R.id.nopay_course_details_tv_title)
    TextView nopay_course_details_tv_title;
    @BindView(R.id.nopay_course_details_tv_price)
    TextView nopay_course_details_tv_price;
    @BindView(R.id.nopay_fragment_details_breif_time)
    TextView nopay_fragment_details_breif_time;
    @BindView(R.id.nopay_fragment_details_breif_desc)
    TextView nopay_fragment_details_breif_desc;
    @BindView(R.id.nopay_fragment_details_breif_address)
    TextView nopay_fragment_details_breif_address;
    @BindView(R.id.nopay_fragment_details_breif_webview)
    WebView nopay_fragment_details_breif_webview;


    private String descUrl;
    private int tabStutusId;

    protected void beforeInit() {
        super.beforeInit();
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("课程详情");
        tabStutusId = getIntent().getIntExtra("courseId", 0);
        initViewtRequest();
        initViewRequestCourse();
        initViewWebView();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_course_details_nopay;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    private void initViewtRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).getUrlRequestCoursedetailMain(tabStutusId, new Subscriber<ResponseBody>() {
            @Override
            public void onStart() {
                super.onStart();
                LoadingDialog.showDialogForLoading(CourseDetailsNoPayActivity.this);
            }

            @Override
            public void onCompleted() {
                LoadingDialog.cancelDialogForLoading();

            }

            @Override
            public void onError(Throwable e) {
                LoadingDialog.cancelDialogForLoading();
                ToastUitl.showShort(e.getMessage());
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                LoadingDialog.cancelDialogForLoading();
                try {
                    Gson gson = new Gson();
                    CourseHomeBean courseHomeBean = gson.fromJson(responseBody.string(), CourseHomeBean.class);
                    if (courseHomeBean.getError_code() == 0) {
                        nopay_course_details_tv_title.setText(courseHomeBean.getData().getCoursename());
                        nopay_course_details_tv_price.setText("¥" + courseHomeBean.getData().getCourseprice());
                        if (courseHomeBean.getData().getCoursePhotos().size() > 0) {
                            Picasso.get().load(BaseContacts.BASE_URL + courseHomeBean.getData().getCoursePhotos().get(0).getPhoto())
                                    .into(nopay_course_details_iv_icon);
                        }
                    } else {
                        ToastUitl.showShort(courseHomeBean.getError_message());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    public String getCourseId() {
        return String.valueOf(tabStutusId);
    }

    @OnClick(R.id.nopay_fragment_details_breif_desc)
    public void initOnClickDesc(){
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), WebViewMarkActivity.class);
        intent.putExtra("url", BaseContacts.BASE_URL + descUrl);
        startActivity(intent);

    }

    private void initViewRequestCourse() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .getUrlRequestCoursedetailbrief(tabStutusId,
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();

                            }

                            @Override
                            public void onCompleted() {


                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {

                                try {
                                    Gson gson = new Gson();
                                    BreifBean breifBean = gson.fromJson(responseBody.string(), BreifBean.class);
                                    if (breifBean.getError_code() == 0) {
                                        nopay_fragment_details_breif_time.setText("开课时间： "+ breifBean.getData().getCourse().getCoursestarttime());
                                        nopay_fragment_details_breif_address.setText("开课地点： " + breifBean.getData().getCourse().getCoursesite());
                                        nopay_fragment_details_breif_desc.setText(""+
                                                breifBean.getData().getCoursedatalist().get(0).getTitle());
                                        descUrl = breifBean.getData().getCoursedatalist().get(0).getSrc();
                                    } else {
                                        ToastUitl.showShort(breifBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });


    }

    private void initViewWebView() {
        nopay_fragment_details_breif_webview.loadUrl(BaseContacts.URL_COURSE_ICON_DETAILS + tabStutusId);
        WebSettings settings = nopay_fragment_details_breif_webview.getSettings();
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(true);

        nopay_fragment_details_breif_webview.setWebChromeClient(new CourseDetailsNoPayActivity.MyWebChrome());
        nopay_fragment_details_breif_webview.setWebViewClient(new CourseDetailsNoPayActivity.MyWebClient());
    }

    private class MyWebChrome extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }
    }


    private class MyWebClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LogUtils.logd("截取URL", url);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }
    }



}
